/* 
 * File:   ServiceLocator.cpp
 * Author: alexey
 * 
 * Created on 31 Март 2011 г., 1:41
 */

#include <stdexcept>

#include "servicelocator.h"
using namespace storagemon;
ServiceLocator::ServiceLocator(IServiceLocator *service_locator)
: m_sl(service_locator)
{
}

ServiceLocator::~ServiceLocator()
{
}

void ServiceLocator::RegisterService(const char* service_name,
                IService* service)
{
    if (!service)
        throw (std::runtime_error("ServiceLocator::RegisterService. service is empty"));
    if (m_services.find(service_name) != m_services.end())
        throw (std::runtime_error("ServiceLocator::RegisterService. service is already registered"));

    m_services[service_name] = service;
}

void ServiceLocator::UnregisterService(const char* service_name)
{
    std::map<std::string, IService*>::iterator it = m_services.find(service_name);
    if (it == m_services.end())
        throw (std::runtime_error("ServiceLocator::UnregisterService. service not registered"));

    m_services.erase(it);
}

IService* ServiceLocator::GetService(const char* service_name)
{
     if (m_services.find(service_name) != m_services.end())
         return m_services[service_name];
     else
     {
         if (m_sl)
             return m_sl->GetService(service_name);
     }
     throw (std::runtime_error("ServiceLocator::GetService. Service not Registered."));
}
